package com.baymax.study.redisson.infrastructure.locks.lock;

import com.baymax.study.redisson.infrastructure.locks.framework.DistributedLockImplName;
import com.baymax.study.redisson.infrastructure.locks.framework.DistributedReadLock;
import com.baymax.study.redisson.infrastructure.locks.framework.DistributedReadWriteLock;
import com.baymax.study.redisson.infrastructure.locks.framework.DistributedWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DistributedLockRedissonImpl implements DistributedReadWriteLock, InitializingBean {

    @Autowired
    private RedissonClient redissonClient;

    private DistributedReadLock distributedReadLock;

    private DistributedWriteLock distributedWriteLock;

    @Override
    public DistributedLockImplName getImplementFrameworkName() {
        return DistributedLockImplName.REDISSON;
    }

    @Override
    public DistributedReadLock getReadLock() {
        return distributedReadLock;
    }

    @Override
    public DistributedWriteLock getWriteLock() {
        return distributedWriteLock;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        distributedReadLock = new DistributedReadLockImpl(redissonClient);
        distributedWriteLock = new DistributedWriteLockImpl(redissonClient);
    }
}
